﻿using fuzhu;
using Microsoft.IdentityModel.Tokens;
using Org.BouncyCastle.Ocsp;
using SqlSugar;
using System.Data;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using ZDEntity;

namespace Yonyou.U8.BO
{
    /// <summary>
    ///不能直接继承，因为后面还要转换
    /// </summary>
    public class PersonVM
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string xmlConfig { get; set; }
        public static string cDatabase = "";

        public hr_hi_person hrperson { get; set; }
        public Person ywperson { get; set; }


        public PersonVM()
        {
            //conStr
            conStr = CanShu.conStr;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }

        /// <summary>
        /// 导入U8，新增或修改
        /// </summary>
        /// <returns></returns>
        public RetModel2 Import()
        {

            try
            {
                hr_hi_person hr_Hi_Person = GetHRByPsnum();
                RetModel2 RET = new RetModel2();
                DepartmentVM department = new DepartmentVM();
                if (!department.Exists(hrperson.cDept_num))
                {
                    return DBService.ReErrObject("部门编码不存在");
                }

                if (!ExistsHRByPsnum(hrperson.cPsn_Num))
                { 
                    //新增出来
                    RET = HRPSERON_Add(); 
                }
                else
                {
                    //更新
                    RET = HRPSERON_Update(); 
                };

                if(RET.code==200&& ywperson!= null)
                {
                    if (!department.Exists(ywperson.cDepCode))
                    {
                        return DBService.ReErrObject("业务部门编码不存在");
                    }

                    if (!ExistsYWByPsnum(ywperson.cPersonCode))
                    {
                        //新增出来
                        RET = YWPSERON_Add();
                    }
                    else
                    {
                        //更新
                        RET = YWPSERON_Update();
                    };
                }
                insertResult insert = new insertResult()
                {
                    cReCode = RET.code.ToString(),
                    Result = RET.message,
                    cVouCode = hrperson.cPsn_Num

                };
                List<insertResult> lst = new List<insertResult>();
                lst.Add(insert);
                RET.data = lst;
                return RET;
              
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }



        }

        public RetModel2 HRPSERON_Add()
        {
            try
            {
                string cerr;
                string cid;

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    hrperson.pk_hr_hi_person = Guid.NewGuid();
                   int re =   db.Insertable<hr_hi_person>(hrperson).ExecuteCommand();
                    if (re>0)
                    {
                        return DBService.ReOKObject("人员新增成功");
                    }
                    else
                    {
                        return DBService.ReErrObject("人员新增失败");
                    }
                } 
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }

        public RetModel2 YWPSERON_Add()
        {
            try
            {
                string cerr;
                string cid;

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {

                    int re = db.Insertable<Person>(ywperson).ExecuteCommand();
                    if (re > 0)
                    {
                        return DBService.ReOKObject("业务员新增成功");
                    }
                    else
                    {
                        return DBService.ReErrObject("业务员修改失败");
                    }
                }
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }
        }
        /// <summary>
        /// 人员档案 变更
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public RetModel2 HRPSERON_Update()
        {
            try
            {
                string cerr;
                string cid;

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {

                    int re = db.Updateable<hr_hi_person>(hrperson).ExecuteCommand();
                    if (re > 0)
                    {
                        return DBService.ReOKObject("人员更新成功");
                    }
                    else
                    {
                        return DBService.ReErrObject("人员更新失败");
                    }
                }
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }
        /// <summary>
        /// 业务员 更新
        /// </summary>
        /// <returns></returns>
        public RetModel2 YWPSERON_Update()
        {
            try
            {
                string cerr;
                string cid;

                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {

                    int re = db.Updateable<Person>(ywperson).ExecuteCommand();
                    if (re > 0)
                    {
                        return DBService.ReOKObject("业务员更新成功");
                    }
                    else
                    {
                        return DBService.ReErrObject("业务员修改失败");
                    }
                }
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }
        /// <summary>
        /// 检查人员是否存在 hr_hi_person
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public hr_hi_person GetHRByPsnum()
        {
            if (hrperson.cPsn_Num == null)
            {
                return null;
            }
            
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {

                hr_hi_person hr = db.Queryable<hr_hi_person>().Where(x => x.cPsn_Num == hrperson.cPsn_Num).First();
                 return hr; 
            } 
        
        }


        /// <summary>
        /// 检查人员是否存在 hr_hi_person
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool  ExistsHRByPsnum(string cpsn_num)
        {
            if (string.IsNullOrWhiteSpace(cpsn_num))
            {
                return false;
            }

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {

              return  db.Queryable<hr_hi_person>().Where(x => x.cPsn_Num == cpsn_num).Any(); 
            }

        }
        /// <summary>
        /// 检查业务员是否存在  返回person
        /// </summary>
        /// <returns></returns>
        public Person GetYWYByPsnum()
        {
            if (ywperson.cPersonCode == null)
            {
                return null;
            }

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {

                Person hr = db.Queryable<Person>().Where(x => x.cPersonCode == ywperson.cPersonCode).First();
                return hr;
            }

        }

        /// <summary>
        /// 检查业务员是否存在 
        /// </summary>
        /// <param name="cpersoncode"></param>
        /// <returns></returns>
        public bool ExistsYWByPsnum(string cpersoncode)
        {
            if (string.IsNullOrWhiteSpace(cpersoncode))
            {
                return false;
            }

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {

                return db.Queryable<Person>().Where(x => x.cPersonCode == cpersoncode).Any();
            }

        }
    }
}
